package cn.tedu.imop.basic.service.impl;

import cn.tedu.imop.basic.dao.repository.AreaRepository;
import cn.tedu.imop.basic.entity.Area;
import cn.tedu.imop.basic.service.AreaService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AreaServiceImpl implements AreaService {
    private final AreaRepository areaRepository;
    public AreaServiceImpl(AreaRepository areaRepository) {
        this.areaRepository = areaRepository;
    }
    @Override
    public List<Area> getAreaList() {
        List<Area> returnList = new ArrayList<>();
        Map<Long, List<Area>> areaMap = areaRepository.selectAreaList();
        //获取所有一级区域信息
        List<Area> firstLevelAreaList = areaMap.get(0L);
        for(Area area1: firstLevelAreaList){
            returnList.add(area1);
            //获取二级区域信息
            List<Area> secondList=areaMap.get(area1.getId());
            if(CollectionUtils.isEmpty(secondList))continue;
            area1.setChildren(secondList);
            //获取三级区域信息
            for(Area area2:secondList){
                List<Area> thirdList=areaMap.get(area2.getId());
                if(CollectionUtils.isEmpty(thirdList))continue;
                area2.setChildren(thirdList);
            }
        }
        return returnList;
    }
}
